﻿using System;
using System.Windows.Forms;


namespace Marmat.Forms
{
    public class InputValue
    {

        /// <summary>
        /// InputValue函数用于显示一个对话框，获得用户输入的单个值。
        /// </summary>
        /// <param name="variant">用于接收数据的变量</param>
        /// <param name="caption">字符型，设置对话框标题</param>
        /// <param name="description">字符型，设置对话框说明</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static bool Show(ref object variant, string caption = null, string description = null)
        {
            var frm = new InputValueForm(variant, false);
            frm.Text = caption;
            frm.Label1.Text = description;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (!object.ReferenceEquals(frm.Sender_0.Text, DBNull.Value))
                {
                    variant = frm.Sender_0.Value;
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// InputValue函数用于显示一个对话框，获得用户输入的单个值。
        /// </summary>
        /// <param name="variant">用于接收数据的变量</param>
        /// <param name="caption">字符型，设置对话框标题</param>
        /// <param name="description">字符型，设置对话框说明</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static bool Show<TValue>(ref TValue variant, string caption = null, string description = null)
        {
            var frm = new InputValueForm(variant, false);
            frm.Text = caption;
            frm.Label1.Text = description;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (!object.ReferenceEquals(frm.Sender_0.Text, DBNull.Value))
                {
                    variant = (TValue)frm.Sender_0.Value;
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// InputPassword函数用于显示一个对话框，用于输入密码。
        /// 这样我们就无需为一些简单的密码输入工作而专门设计窗口，直接使用InputPassword函数即可。
        /// </summary>
        /// <param name="password">用于接收密码的变量</param>
        /// <param name="caption">字符型，设置对话框标题</param>
        /// <param name="description">字符型，设置对话框说明</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static bool InputPassWord(ref string password, string caption = null, string description = null)
        {
            var frm = new InputValueForm(password, true);
            frm.Text = caption;
            frm.Label1.Text = description;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (!object.ReferenceEquals(frm.Sender_0.Text, DBNull.Value))
                {
                    password = Convert.ToString(frm.Sender_0.Value);
                    return true;
                }
            }

            return false;
        }
    }
}